<template>
  <div>
    <el-card>
      <el-button v-has="'btn.Trademark.add'" type="primary" style="margin-bottom: 20px" @click="addTrade">添加品牌</el-button>

      <el-table :data="tradeTable" style="width: 100%">
        <el-table-column type="index" label="序号" width="180" />
        <el-table-column prop="tmName" label="品牌名称" width="180" />
        <el-table-column prop="logoUrl" label="品牌logo">
          <template #default="{ row }">
            <img :src="row.logoUrl" alt="" style="width: 80px; height: 80px" />
          </template>
        </el-table-column>
        <el-table-column label="品牌操作">
          <template #default="{ row }">
            <el-button v-has="'btn.Trademark.update'" type="primary" icon="Edit" circle @click="editTrade(row)" />
            <el-button v-has="'btn.Trademark.remove'" type="danger" icon="Delete" @click="deleteTrade(row)" circle />
          </template>
        </el-table-column>
      </el-table>

      <el-pagination style="margin-top: 20px" v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[3, 6, 9]" background layout="prev, pager, next,jumper,->,sizes,total" :total="total" @size-change="handleSizeChange" @current-change="getTableData" />
    </el-card>

    <FormDialog ref="dialogRef" :type="type"></FormDialog>
  </div>
</template>

<!-- 品牌管理 -->
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { getTrademark, deleteTrademark } from '@/utils/api/product/trademark/index'
import { TradeMark } from '@/utils/api/product/trademark/type'
import FormDialog from './addOrEdit/index.vue'

let currentPage = ref<number>(1)
let pageSize = ref<number>(3)
let total = ref<number>(0)
let tradeTable = ref<TradeMark[]>([])
let dialogRef = ref()

// 让子组件判断是更新还是xinzeng
const type = ref<string>()

onMounted(() => {
  getTableData()
})

// 获取表格数据
const getTableData = async (page = 1) => {
  currentPage.value = page
  const res = await getTrademark(currentPage.value, pageSize.value)
  if (res.data.code === 200) {
    total.value = res.data.data.total
    // pageSize.value = res.data.data.size
    tradeTable.value = res.data.data.records
  }
}

// 每页展示的数量改变
const handleSizeChange = () => {
  // pageSize.value = size 因为v-model数据双向绑定这里不用再修改了
  getTableData()
}

// 添加品牌
const addTrade = () => {
  type.value = 'add'
  dialogRef.value.cleanFormVaild()
  dialogRef.value.dialogFormVisible = true
}

// 删除商品
const deleteTrade = async (row: TradeMark) => {
  const res = await deleteTrademark(row.id!)
  if (res.data.code == 200) {
    ElMessage.success('删除成功')
    tradeTable.value.length == 1 ? currentPage.value - 1 : ''
    getTableData(currentPage.value)
  }
}

// 编辑商品
const editTrade = (data: TradeMark) => {
  dialogRef.value.cleanFormVaild()
  type.value = 'edit'
  dialogRef.value.form.logoUrl = data.logoUrl
  dialogRef.value.form.tmName = data.tmName
  dialogRef.value.form.id = data.id
  dialogRef.value.dialogFormVisible = true
}

// 暴露给子组件
defineExpose({ getTableData, tradeTable, currentPage })
</script>

<style scoped></style>
